define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reimburserecord/index',
                    add_url: 'reimburserecord/add',
                    edit_url: 'reimburserecord/edit',
                    del_url: 'reimburserecord/del',
                    multi_url: 'reimburserecord/multi',
                    table: 'reimburse_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merid', title: __('Merid')},
                        {field: 'user.username', title: __('User_id')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order_amount', title: __('Order_amount'), operate:'BETWEEN'},
                        {field: 'reimburse_amount', title: __('Reimburse_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), visible: false, searchList: Config.statusList},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                         {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                // {name: 'cancel', text: '取消订单', title: '取消订单', icon: '', classname: 'btn btn-xs btn-danger btn-addtabs', url: 'order/edit',},
                                {
                                    name: 'agree', 
                                    text: '同意报销', 
                                    title: '同意报销', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-ajax', 
                                    confirm: '确认报销该申请吗？', 
                                    url: 'reimburserecord/agree',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 0){
                                    $(table).data("operate-agree", null);
                                }

                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});